package cn.liu.hrm.service.impl;

import cn.liu.hrm.doc.CourseDoc;
import cn.liu.hrm.domain.Course;
import cn.liu.hrm.domain.CourseDetail;
import cn.liu.hrm.domain.CourseMarket;
import cn.liu.hrm.dto.CourseDto;
import cn.liu.hrm.dto.LoginInfoDto;
import cn.liu.hrm.feignclient.ESFeignClient;
import cn.liu.hrm.mapper.CourseDetailMapper;
import cn.liu.hrm.mapper.CourseMapper;
import cn.liu.hrm.mapper.CourseMarketMapper;
import cn.liu.hrm.query.CourseQuery;
import cn.liu.hrm.service.ICourseService;
import cn.liu.hrm.util.AjaxResult;
import cn.liu.hrm.util.PageList;
import cn.liu.hrm.util.UserContext;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.liu
 * @since 2020-03-28
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Autowired
    private CourseDetailMapper courseDetailMapper ;

    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseDto courseDto) {
        Course course = courseDto.getCourse();
        CourseDetail courseDetail = courseDto.getCourseDetail();
        CourseMarket courseMarket = courseDto.getCourseMarket();
        //1.判断参数是否合法
        if(!StringUtils.hasLength(course.getName())){
            throw new RuntimeException("课程名字不能为空");
        }
        //2.保存课程表
        //状态
        course.setStatus(Course.STATUS_OFFLINE);
        //获取当前的用户------获取用户的机构 ： UserContext -> session  ;
        LoginInfoDto loginInfo = UserContext.getLoginInfo();

        //机构id
        course.setTenantId(loginInfo.getTenantId());

        //机构名字
        course.setTenantName(loginInfo.getTenantName());

        //创建用户的id
        course.setUserId(loginInfo.getUserId());

        //创建用户的名字
        course.setUserName(loginInfo.getUsername());

        Date date = new Date();
        course.setStartTime(date);
        course.setEndTime(date);

        baseMapper.insert(course);
        //3.保存课程营销表
        courseMarket.setId(course.getId());
        courseMarket.setValid(0);
        courseMarket.setExpires(date);
        courseMarket.setPriceOld(courseMarket.getPrice()+100);
        courseMarketMapper.insert(courseMarket);

        //4.保存课程详情表
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
    }
    //课程上线
    //上线课程
    @Override
    public void onLineCourse(Long id) {
        //1.根据课程ID查询 课程对象
        Course course = baseMapper.selectById(id);

        //2.判断：课程不能为空 ， 课程状态必须是下线状态
        if(course == null || course.getStatus() != 0){
            throw new RuntimeException("课程为空，或者状态异常");
        }

        //3.把课程存储到ES
        //3.1.编写ES的Feign

        //3.2.把课程 Course 转成 CourseDoc ，
        //课程营销
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        //课程详情
        CourseDetail courseDetail = courseDetailMapper.selectById(id);

        //将内容装进CourseDoc,通过工具的属性拷贝
        CourseDoc courseDoc = new CourseDoc();

        BeanUtils.copyProperties(courseDoc,course);
        BeanUtils.copyProperties(courseMarket,course);
        BeanUtils.copyProperties(courseDetail,course);
        // 门户网站需要什么内容我们就放什么内容到CourseDoc

        //3.3.调用ES的Feign添加CourseDoc数据到ES
        AjaxResult saveAjaxResult = esFeignClient.save(courseDoc);
        if (!saveAjaxResult.isSuccess()){
            throw new RuntimeException("课程上线失败["+saveAjaxResult.getMessage()+"]");
        }
        //4.修改Mysql中的ES状态为已上线
        course.setStatus(1);
        baseMapper.updateById(course);
    }

    //课程下线的方法
    @Override
    public void offLineCourse(Long id) {
        //1.判断参数

        //2.删除ES中的课程
        AjaxResult ajaxResult = esFeignClient.deleteById(id);
        if (!ajaxResult.isSuccess()){
            throw new RuntimeException("课程下线失败["+ajaxResult.getMessage()+"]");
        }

        //3.修改MySQL中的课程状态
        baseMapper.updateStatusById(id,0);
    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        //1.查询条件，把CourseQuery封装成ES的查询对象

        //2.执行查询

        //3.结果处理，把page封装成pageList
        return esFeignClient.searchCourse(courseQuery);
    }
}
